package com.xls.boxlibrary;

import android.app.Activity;
import android.content.Context;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.xls.boxlibrary.Consts.StateConsts;
import com.xls.boxlibrary.duihuanjibi.DuiHuanJiBiActivity;
import com.xls.boxlibrary.network.RequestCallBack;
import com.xls.boxlibrary.network.RequestManager;
import com.xls.boxlibrary.network.RetrofitBase;
import com.xls.boxlibrary.utils.SPUtils;

import java.util.Map;

/**
 * 提现工具类
 */
public class WithdrawalUtil {
    /**
    * 单例模式
    **/
    private static WithdrawalUtil mWithdrawalUtil = null;
        /**
         * 构造函数私有化
         **/
        private WithdrawalUtil() {
        }
        /**
         * 公有的静态函数，对外暴露获取单例对象的接口
         **/
        public static WithdrawalUtil getInstance() {
            if (mWithdrawalUtil == null) {
                synchronized (WithdrawalUtil.class) {
                    if (mWithdrawalUtil == null) {
                        mWithdrawalUtil = new WithdrawalUtil();
                    }
                }
            }
            return mWithdrawalUtil;
        }

        public void startWithdrawalActivity(Activity activity,String appid, String packageName){
            initImageLoader(activity);
            DuiHuanJiBiActivity.startActivity(activity,appid,packageName, StateConsts.TiXianType.ShouYi);
        }

    private void initImageLoader(Context context) {
        // 创建DisplayImageOptions对象
        DisplayImageOptions defaulOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisk(true).build();
        // 创建ImageLoaderConfiguration对象
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
                .defaultDisplayImageOptions(defaulOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
        // ImageLoader对象的配置
        ImageLoader.getInstance().init(configuration);

    }

    /**
     * 登陆盒子
     * @param params
     */
    public void loginBox(Activity activity,Map<String, String> params) {
        RetrofitBase.getInstace(activity).setSubscribe(activity, RequestManager.getInstance(activity).
                getLogin(params), new RequestCallBack<String>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(String requestData) {
                SPUtils.saveLoginToken(activity,requestData);
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {
                SPUtils.saveLoginToken(activity,"");
            }
        });
    }
}
